//
//  ShenSuoTableController.m
//  OCDemo
//
//  Created by BH on 15/10/19.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "ShenSuoTableController.h"
#import "ShenSuoTableCell.h"
#import <Masonry/Masonry.h>

@interface ShenSuoTableController ()<UITableViewDataSource,UITableViewDelegate>
{
    CGRect bounds;
    NSMutableArray *titleArray;//标题
    NSMutableArray * rowHight;//行高
    NSMutableArray *bCheck;   // 所点按钮
}

@end

@implementation ShenSuoTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleArray = [NSMutableArray array];
    rowHight = [NSMutableArray array];
    bCheck = [NSMutableArray array];
    bounds = self.view.frame;
    
    [titleArray addObject:@"【农民收入恐损千亿 三大主粮收购价全线下跌】粮食主产区的玉米小麦价值均出现不同程度下跌。玉米平均下跌20%以上，最高跌幅相比去年达30%。分析师在接受华夏时报采访时说，今年要格外重视粮价大跌对数亿农民收入的影响，千亿级的收入负增长会影响到消费市场，预计会影响1%GDP。"];
    [titleArray addObject:@"【新加坡支持中国促进亚洲稳定】新加坡国防部长黄永宏医生今天在第六届香山论坛致辞时说，新加坡支持中国在促进亚洲稳定安全方面发挥领导作用。黄永宏指出，中国昨天在中国－亚细安防长非正式会晤提出的多项倡议具操作性，将有助建立战略互信。"];
    [titleArray addObject:@"【【南京：法国梧桐树腐烂后沦为垃圾箱】法桐原本是南京的一大特色风景线，但因各种原因很多法桐出现腐烂空洞的现象，低处的树洞甚至成了行人投放垃圾、烟头的垃圾桶和烟灰缸。香烟头、果皮、纸杯、饮料水瓶……树干里满是行人随手丢弃的各式垃圾。别让城市原本的风景毁掉！"];
    [titleArray addObject:@"【村民上百件藏品被扣25年 警方：已交博物馆不会归还】湖南村民潘泽黄1990年遭人举报“倒卖文物”，收藏的100多件藏品被公安扣押25年。当地公安回应，藏品已移交博物馆。也不再对事件进行调查。博物馆则称，所扣物品不适合个人收藏，已登记档案，归属于博物馆，不再归还。"];
    [titleArray addObject:@"【女子闯红灯被处罚 耍赖称被交警“袭胸”】河南洛阳一女子骑电动车闯红灯被抓现行."];
    [titleArray addObject:@"【洪秀柱在国民党临全会演讲全文】洪秀柱称，做为参选人，希望再进一步，为两岸创造和平，这是台湾地区领导人责无旁贷的使命。因此洪秀柱提出在五个前提下签署两岸和平协定，这不是投降，更不是卖台，而是台湾人民同命共生的愿力！"];
    
    for (int i = 0; i<titleArray.count; i++) {
        [bCheck addObject:[NSNumber numberWithInt:0]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - tableview datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ShenSuoTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ShenSuoTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier text:titleArray[indexPath.row] frame:bounds];
        
        ShenSuoButton *cellBtn = cell.btn;
        cellBtn.tag = indexPath.row+200;
        [cellBtn addTarget:self action:@selector(cellBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //行高
        [rowHight addObject:[NSNumber numberWithFloat:[cell returnRowHeight]]];
        
        cell.label.tag = indexPath.row+100;
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (bCheck[indexPath.row] == [NSNumber numberWithInt:1]) {
        return [rowHight[indexPath.row] floatValue];
    }else{
    
        return 60;
    }
}

-(void)cellBtnClick:(ShenSuoButton *)sender
{
    //初始化,所有单元格收缩到原始状态
    [bCheck removeAllObjects];
    for (int i = 0; i<rowHight.count; i++) {
        [bCheck addObject:[NSNumber numberWithBool:NO]];
    }
    
    if (sender.isEXP) {
        [bCheck replaceObjectAtIndex:sender.tag-200 withObject:[NSNumber numberWithInt:1]];
    }
    else
    {
        [bCheck replaceObjectAtIndex:sender.tag-200 withObject:[NSNumber numberWithInt:0]];
    }
    
    for (int i = 0; i<rowHight.count; i++) {
        ShenSuoButton *ssBtn = (ShenSuoButton *)[self.view viewWithTag:200+i];
        
        if ([ssBtn respondsToSelector:@selector(setBtnStatusWithImageName:)]) {
            [ssBtn setIsEXP:YES];
            [ssBtn setBtnStatusWithImageName:@"double-down@2x"];
            NSLog(@"btn = %d",i);
        }
        
        UILabel *label = (UILabel *)[self.view viewWithTag:100+i];
        label.numberOfLines = 2;
        [label sizeToFit];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@5);
            make.top.equalTo(@2);
            make.width.equalTo(@(bounds.size.width-45));
            make.height.equalTo(@40);
        }];
    }
    
    //选中行进行变化
    if (bCheck[sender.tag-200] == [NSNumber numberWithInt:1]) {
        UILabel *label = (UILabel *)[self.view viewWithTag:sender.tag-100];
        label.numberOfLines = 0;
        [label sizeToFit];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.height.equalTo(@([rowHight[sender.tag-200] floatValue]));
        }];
        
        [sender setIsEXP:NO];
        [sender setBtnStatusWithImageName:@"double-up@2x"];
    }else{
    
        UILabel *label = (UILabel *)[self.view viewWithTag:sender.tag-100];
        label.numberOfLines = 2;
        [label sizeToFit];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@40);
        }];
        
        [sender setIsEXP:YES];
        [sender setBtnStatusWithImageName:@"double-down@2x"];
    }
    
    [self.table reloadData];
}


@end
